class Solution
{
public:
    int minAnagramLength(string s)
    {
        int n = s.size();
        vector<vector<int>> charCount(n + 1, vector<int>(26, 0));
        for (int i = 0; i < n; i++)
        {
            charCount[i + 1] = charCount[i];
            charCount[i + 1][s[i] - 'a']++;
        }

        for (int len = 1; len < n; len++)
        {
            if (n % len != 0)
            {
                continue;
            }
            vector<int> diff = charCount[len];
            bool isAnagram = true;
            for (int start = len; start + len <= n; start += len)
            {
                if (!isAnagram)
                {
                    break;
                }

                for (int i = 0; i < 26; i++)
                {
                    if (diff[i] != charCount[start + len][i] - charCount[start][i])
                    {
                        isAnagram = false;
                        break;
                    }
                }
            }
            if (isAnagram)
            {
                return len;
            }
        }
        return n;
    }
};